package com.gouqi.bookdemobackend.controller;


import com.gouqi.bookdemobackend.common.BaseResponse;
import com.gouqi.bookdemobackend.common.ErrorCode;
import com.gouqi.bookdemobackend.common.ResultUtils;
import com.gouqi.bookdemobackend.exception.BusinessException;
import com.gouqi.bookdemobackend.model.dto.UserLoginRequest;
import com.gouqi.bookdemobackend.model.vo.UserVo;
import com.gouqi.bookdemobackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<UserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVo user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }

}
